﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Security;
using System.Web.Services;
using System.Collections.ObjectModel; 
using System.Text;
using System.Configuration;

namespace TractRecord
{
    public partial class Default : TractRecordBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["approved"]) == true)
            {
                Session["approved"] = null;
                RegisterJavaScriptVariable("approved", "true");
            }

            if (Membership.GetUser() != null)
            {
                Response.Redirect("/Authenticated/Default.aspx");
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static Boolean ValidateLogin(string UserName, string Password)
        {
            return Membership.ValidateUser(UserName, Password);
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string ValidateNewUser(string UserName, string Password, string Email, string Question, string Answer)
        {
            MembershipCreateStatus status = new MembershipCreateStatus();

            try
            {

                MembershipUser NewUser = Membership.CreateUser(UserName, Password, Email, Question, Answer, false, out status);

                StringBuilder sb = new StringBuilder();

                sb.Append("New user creation, ");
                sb.Append("[UserName] = '" + UserName + "'");
                sb.Append("[Password] = '" + Password + "'");
                sb.Append("[Email] = '" + Email + "'");
                sb.Append("[Question] = '" + Question + "'");
                sb.Append("[Answer] = '" + Answer + "'");
                sb.Append(", completed with status: " + status.ToString());

                Code.Common.Log(sb.ToString(), "Audit");

                if (status == MembershipCreateStatus.Success)
                {
                    Roles.AddUserToRole(NewUser.UserName, ConfigurationManager.AppSettings["DefaultUserRole"]);

                    //Send email verfication email

                    string verifyUrl = HttpContext.Current.Request.Url.Scheme + "://" +
                        HttpContext.Current.Request.Url.DnsSafeHost +
                        (HttpContext.Current.Request.Url.IsDefaultPort ? "" : ":" + HttpContext.Current.Request.Url.Port).ToString() +
                        HttpContext.Current.Request.ApplicationPath +
                        "Process/Verify.aspx";


                    ListDictionary replacements = new ListDictionary();
                    replacements.Add("<%Name%>", UserName);
                    replacements.Add("<%VerifyUrl%>", verifyUrl + "?key=" + NewUser.ProviderUserKey);

                    WebCommon.GenerateEmailMessage(WebCommon.MailMessageType.NewUser, "New Account Verification", Email, replacements);


                }
            }
            catch (Exception ex)
            {
                status = MembershipCreateStatus.ProviderError;
                Code.Common.Log(ex.ToString(), "Default.aspx.cs_ValidateNewUser", 100, 0, System.Diagnostics.TraceEventType.Critical, "Default.aspx.cs_ValidateNewUser");
            }
            return status.ToString();

        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Log(string Message, string Category, int Priority)
        {
            Code.Common.Log(Message, Category, Priority);

        }
    }
}